{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "62a31b37",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "Do imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7a3a70d",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import io\n",
    "import os\n",
    "from functools import reduce\n",
    "from fal.dbt.integration.magics import init_fal"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "256f078d",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "Initiate FalDbt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a3d27de",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%init_fal project_dir=../.. profiles_dir=../../.. default_model_name=zendesk_ticket_data"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e3cc7e7c",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "Function for finding a model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f998243f",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def find_model(model_name: str):\n",
    "    models = list_models()\n",
    "    return [model for model in models if model.name == model_name][0]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e32e0e2b",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "Function for writing dataframe data in a file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "021f4d26",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def check_and_write_data(model_name: str):\n",
    "    output = f\"Model name: {model_name}\"\n",
    "    model = find_model(model_name)\n",
    "    output = output + f\"\\nStatus: {model.status}\"\n",
    "    df = ref(model_name)\n",
    "    df.columns = df.columns.str.lower()\n",
    "\n",
    "    if hasattr(df, \"extra_col\"):\n",
    "        output = f\"extra_col: {df.extra_col[0]}\\n\"\n",
    "    else:\n",
    "        output = \"no extra_col\\n\"\n",
    "\n",
    "\n",
    "    temp_dir = os.environ[\"temp_dir\"]\n",
    "    write_dir = open(reduce(os.path.join, [temp_dir, model_name + \".my_notebook.txt\"]), \"w\")\n",
    "    write_dir.write(output)\n",
    "    write_dir.close()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "41f47376",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "Model name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c99dab1",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model_name = \"zendesk_ticket_data\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f8e7490d",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "Process dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f3e47dc",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "check_and_write_data(model_name)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "argv": [
    "python",
    "-m",
    "ipykernel_launcher",
    "-f",
    "{connection_file}"
   ],
   "display_name": "Python 3 (ipykernel)",
   "env": null,
   "interrupt_mode": "signal",
   "language": "python",
   "metadata": {
    "debugger": true
   },
   "name": "python3"
  },
  "name": "my_notebook.ipynb"
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
